﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using log4net.Config;
using log4net.Appender;
using log4net.Repository.Hierarchy;
using log4net;

/// <summary>
/// Log4net 的摘要描述
/// </summary>
/// 
namespace com.pro2e.console.generic.log
{
    public class Log4net
    {
        private static string LOG_FILE = "/App_Data/Log/Log.txt";
        public Log4net()
        {
          
        }

        /// <summary>
        /// 一般log4net基本的config設定
        /// </summary>
        public static void WebConfigBaseConfig()
        {           
            log4net.Config.XmlConfigurator.Configure();
        }

        /// <summary>
        /// 一般log4net基本的config設定
        /// </summary>
        public static void BaseConfig()
        {
            BasicConfigurator.Configure();
          
        }

        /// <summary>
        /// 使用xml檔案做log4net的設定檔
        /// </summary>
        /// <param name="filePath">設定檔的位置路徑</param>
        public static void FileConfig(string filePath)
        {
            XmlConfigurator.Configure(new System.IO.FileInfo(filePath));
          
        }

        public static void AddFileAppender()
        {
            Logger root;
            root = ((Hierarchy)LogManager.GetRepository()).Root;        
            root.AddAppender(GetFileAppender());
            root.Repository.Configured = true;

        }
        private static FileAppender GetFileAppender()
        {
            FileAppender lAppender = new FileAppender();
            lAppender.Name = "File";
            lAppender.AppendToFile = true;
            lAppender.File = LOG_FILE;
            lAppender.Layout = new log4net.Layout.PatternLayout("%date{dd-MM-yyyy HH:mm:ss,fff} %5level [%2thread] %message (%logger{1}:%line)%n");
            lAppender.Threshold = log4net.Core.Level.All;
            lAppender.ActivateOptions();
            return lAppender;
        }
    }
}